from django.shortcuts import render,redirect

# Create your views here.
from django.http import HttpResponse
from .models import ArticlePost,ArticleColumn
from .forms import ArticlePostForm
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.db.models import Q
from comment.models import Comment


@login_required(login_url='/userprofile/login/')
def article_delete(request,id):
	article = ArticlePost.objects.get(id=id)
	if request.user != article.author:
		return HttpResponse("无权修改")
	article.delete()
	return redirect("article:article_list")

@login_required(login_url='/userprofile/login/')
def article_update(request,id):
	""" post提交表单修改   get进入初始界面 """
	article = ArticlePost.objects.get(id=id)
	if request.user != article.author:
		return HttpResponse("无权修改")
	if request.method == "POST":
		article_post_form = ArticlePostForm(data=request.POST)
		if article_post_form.is_valid():
			#user = User.objects.get(id=article.author)
			article.title = request.POST['title']
			article.body = request.POST['body']

			if request.POST['column'] != 'none':
				article.column = ArticleColumn.object.get(id=request.POST['column'])
			else:
				article.column = None

			article.save()
			return redirect("article:article_detail",id=id)
		else:
			return redirect("提交内容有错，请查正")
	else:
		article_post_form = ArticlePostForm()
		columns = ArticleColumn.objects.all()
		context = {'article':article,'article_post_form':article_post_form,'columns':columns}
		return render(request,'article/update.html',context)

@login_required(login_url='/userprofile/login/')
def article_create(request):
	if request.method == "POST":
		article_post_form = ArticlePostForm(data = request.POST)
		if article_post_form.is_valid():
			new_article = article_post_form.save(commit=False)
			new_article.author = User.objects.get(id=request.user.id)

			if request.POST['column'] != 'none':
				new_article.column = ArticleColumn.objects.get(id=request.POST['column'])

			new_article.save()
			return redirect("article:article_list")
		else:
			return HttpResponse("表单内容有误，请重新填写")
	else:
		article_post_form = ArticlePostForm()
		columns = ArticleColumn.objects.all()
		context = {'article_post_form':article_post_form,'columns':columns}
		return render(request,'article/create.html',context)

import markdown
def article_list(request):
	"""不分页
	articles = ArticlePost.objects.all()
	
	context = {'articles':articles} 
	print("context:",context)
	return render(request,'article/list.html',context)
	"""
	'''分页
	article_list = ArticlePost.objects.all()
	paginator = Paginator(article_list,1)
	page = request.GET.get('page')
	articles = paginator.get_page(page)
	context = {'articles':articles}
	return render(request,'article/list.html',context)
	'''
	'''按照请求参数返回列表
	if request.GET.get('order') == 'total_views':
		article_list = ArticlePost.objects.all().order_by('-total_views')
		order = 'total_views'
	else:
		article_list = ArticlePost.objects.all()
		order = 'normal'
	paginator = Paginator(article_list,3)
	page = request.GET.get('page')
	articles = paginator.get_page(page)
	context = { 'articles':articles,'order':order }
	return render(request,'article/list.html',context)
	'''
	'''搜索条件'''
	
	order = request.GET.get('order')
	search = request.GET.get('search')
	if search:
		if order == 'total_views': #按照热度排行
			article_list = ArticlePost.objects.filter(Q(title__icontains=search)|
														Q(body__icontains=search)).order_by('-total_views')
		else:
			article_list = ArticlePost.objects.filter(Q(title__icontains=search)|
														Q(body__icontains=search))
	else:
		search=''
		if order == 'total_views':
			article_list = ArticlePost.objects.all().order_by('-total_views')
		else:
			article_list = ArticlePost.objects.all()
	paginator = Paginator(article_list,3)
	page = request.GET.get('page')
	articles = paginator.get_page(page)
	context = { 'articles':articles,'order':order,'search':search }
	return render(request,'article/list.html',context)

def article_detail(request,id):
	article = ArticlePost.objects.get(id=id)

	comments = Comment.objects.filter(article=id)

	article.total_views += 1
	article.save(update_fields=['total_views'])
	'''
	article.body = markdown.markdown(article.body,
		extensions=[
		'markdown.extensions.extra',
		'markdown.extensions.codehilite',
		#目录扩展
		'markdown.extensions.toc',
		])
		'''
	md = markdown.Markdown(
		extensions = [
					'markdown.extensions.extra',
		'markdown.extensions.codehilite',
		#目录扩展
		'markdown.extensions.toc',
		]
	)
	article.body = md.convert(article.body)
	context = {'article':article,'toc':md.toc,'comments':comments}
	return render(request,'article/detail.html',context)